package com.api.syh56.shengyunhui.config;


import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始插入填充...");
        System.out.println(LocalDateTime.now());
//        this.strictInsertFill(metaObject, "creation_time", LocalDateTime.class, LocalDateTime.now());
//        System.out.println(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        if( !Objects.isNull(BaseUserInfo.get(UserInfoEnu.id.getCode())) ){
            this.setFieldValByName("creatorUserId", Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode())), metaObject);
            this.setFieldValByName("lastModifierUserId", Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode())), metaObject);
        }

        this.setFieldValByName("creationTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("lastModificationTime", LocalDateTime.now(), metaObject);
//        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
//        this.setFieldValByName("updateUser", "", metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新填充...");
        if( !Objects.isNull(BaseUserInfo.get(UserInfoEnu.id.getCode())) ){
            this.setFieldValByName("lastModifierUserId", Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode())), metaObject);
        }
        this.setFieldValByName("lastModificationTime", LocalDateTime.now(), metaObject);
    }
}